//
//  MainTabbar.m
//  K8彩票
//
//  Created by zhoubo on 2017/6/6.
//  Copyright © 2017年 zhoubo. All rights reserved.
//

#import "MainTabbar.h"
#import "TabbarButton.h"
#import "ViewController.h"
#import "Config.h"

#import "ComposeViewController.h"

#import "NewsHomeViewController.h"




static int NUM = 4;
@interface MainTabbar()

@property(nonatomic,weak) ViewController* tabbarCon;

@property(nonatomic,strong) NSArray<UIViewController *>* childControllers;

@property(nonatomic,strong) NSMutableArray<TabbarButton*>* buttons;

@property (nonatomic,strong) TabbarButton* composeBtn;

@end

@implementation MainTabbar

-(instancetype)initWithController:(ViewController*)controller andChildControllers:(NSArray*)controllers;
{
    if (self = [super init]) {
        self.tabbarCon = controller;
        self.childControllers = controllers;
        self.buttons = [[NSMutableArray alloc]init];
        
        [self setup];
    }
    return self;
}

-(instancetype)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super initWithCoder:aDecoder]) {
        
//        [self setup];
    }
    return self;
}

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
//        [self setup];
    }
    return self;
}

-(void)setup
{
    NSArray* imgs = @[@"home",@"hall",@"find",@"profile"];
    
    
    NSArray* titles = @[NSLocalizedString(@"主页", @"主页"),@"资讯",NSLocalizedString(@"历史开奖", @"历史开奖"),NSLocalizedString(@"个人", @"个人")];
    
    self.backgroundColor = [UIColor whiteColor];
    
    [self setupChildViewControllers:self.childControllers andImgNames:imgs andTitles:titles];
    
    TabbarButton* btn = self.buttons[0];
    btn.selected = YES;
    btn.userInteractionEnabled = false;

}

-(void)setupChildViewControllers:(NSArray*)childControllers andImgNames:(NSArray*)imgs andTitles:(NSArray*)titles
{
    
    // 顶部分割线
    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth(), 0.5)];;
    [line setBackgroundColor:[UIColor grayColor]];
    [self addSubview:line];
    
    for (int i = 0 ; i < childControllers.count; i++) {
        
        
        
        
      
        

        // 设置tabbar button
        [self addTabbarButtons:i imgNames:imgs titles:titles];
        UIViewController* childCon = childControllers[i];
        childCon.title = titles[i];
        UINavigationController* navCon = nil;
        
        if ([childCon isKindOfClass:[UINavigationController class]]) {

            navCon = (UINavigationController*)childCon;
        }
        else
        {
            navCon = [[UINavigationController alloc]initWithRootViewController:childCon];
        }
        
        [self.tabbarCon addChildViewController:navCon];
        
        if (NUM == 5) {
            // 添加中间按钮
            [self addcomposeButton];
        }

        
        

        
    }
}


-(void)addcomposeButton
{
    
    CGFloat width = [UIScreen mainScreen].bounds.size.width/5;
    self.composeBtn.frame =  CGRectMake(width*2,0,width,49);

    self.composeBtn.clickhandle = ^{
        
        
        UIStoryboard* sb = [UIStoryboard storyboardWithName:@"NewsHome" bundle:nil];
        UIViewController* con = sb.instantiateInitialViewController;
        [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:con animated:YES completion:nil];
    };

}

-(void)addTabbarButtons:(NSInteger)index imgNames:(NSArray*)imgNames titles:(NSArray*)titles
{
    CGFloat WIDTH = [UIScreen mainScreen].bounds.size.width / NUM
    ;
    CGFloat HEIGHT = 49;
    
    NSInteger offsetIndex = index<2?index:index+1;
    
    if (NUM == 4) {
        offsetIndex = index;
    }

    CGFloat x = offsetIndex*WIDTH;
    CGFloat y = 0;
    CGFloat width = WIDTH;
    CGFloat height = HEIGHT;
    
    
    
    
    TabbarButton* btn = [[TabbarButton alloc]init];
    [self.buttons addObject:btn];
    btn.frame = CGRectMake(x, y, width, height);
    [btn setImage:[UIImage imageNamed:imgNames[index]] forState:UIControlStateNormal];
    
    NSString* selName = [NSString stringWithFormat:@"%@%@",imgNames[index],@"_sel"];
    [btn setImage:[UIImage imageNamed:selName] forState:UIControlStateSelected];
    
    [btn setTitle:titles[index] forState:UIControlStateNormal];
    btn.adjustsImageWhenHighlighted = false ;
    
    btn.tag = index + 100;
    
    
    [self addSubview:btn];
    
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    

    
}

-(void)btnClick:(UIButton*)sender
{
    NSLog(@"tabbar button click");
    
    NSInteger index = sender.tag - 100;
    
    [self.tabbarCon showController:index];

    sender.selected = true;
    sender.userInteractionEnabled = false ;
    for (UIButton* btn in self.buttons) {
        if (btn != sender) {
            btn.selected = false;
            btn.userInteractionEnabled = true;
        }
    }
    
}


-(TabbarButton *)composeBtn
{
    if (_composeBtn == nil) {
        _composeBtn = [[TabbarButton alloc]  init];
        [_composeBtn setImage:[UIImage imageNamed:@"post_normal"] forState:UIControlStateNormal];
//        [_composeBtn setImage:[UIImage imageNamed:@"post_normal"] forState:UIControlStateHighlighted];
        _composeBtn.isNeedUp = YES;
//        _composeBtn.backgroundColor = [UIColor redColor];
        [self addSubview:_composeBtn];
    }
    return _composeBtn;
}



/*
 
 
 private lazy var composeButton : TabBarButton = {
 let btn = TabBarButton()
 
 let bacgroundimageNor = UIImage(named: "tabbar_compose_button")
 let bacgroundimageHig = UIImage(named: "tabbar_compose_button_highlighted")
 let imgNor = UIImage(named: "tabbar_compose_icon_add")
 let imgHig = UIImage(named: "tabbar_compose_icon_add_highlighted")
 
 let imageX = UIImage(named: "post_normal")
 
 //        btn.setBackgroundImage(bacgroundimageNor, for: UIControlState.normal)
 //        btn.setBackgroundImage(bacgroundimageHig, for: UIControlState.highlighted)
 
 // 如果想要实现按钮上浮的功能的话, 最好不要同时设置imageView 和 backgroundImageView 如果同时设置这两个属性的话 , 系统会在button的contentview层面上产生两个uiiamgeview对象(打开层级结构可以查看), 这样我们在上浮的话 (这里是设置button的imageView上浮, 背景图片并不会上浮)
 btn.setImage(imageX, for: UIControlState.normal)
 // 这里设置highlighted的图片和normal的相同是为了取消按钮点击时候产生发灰的效果
 btn.setImage(imageX, for: UIControlState.highlighted)
 
 btn.isNeedUp = true
 
 self.addSubview(btn)
 
 
 return btn
 }()
 

 
 
 */



@end
